package com.haircut.demo;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

public class Test {
    public static void hand(int[][] nums) {
        if(nums==null || nums.length==0 || nums[0].length==0) {
            return;
        }

        int m = nums.length;
        int n = nums[0].length;

        int[][] dir = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
        int count = 0;

        int row = 0;
        int col = n-1;

        int[][] visited = new int[m][n];
        int total = 1;

        while(total<m*n) {
            System.out.println(nums[row][col]);
            visited[row][col] = 1;
            if(row+dir[count][0]<0 || row+dir[count][0]>=m || col+dir[count][1] < 0 || col+dir[count][0]>=n || visited[row+dir[count][0]][row+dir[count][0]]==1) {
                count = (count+1)%4;
            }
            row = row+dir[count][0];
            col = col+dir[count][0];
            total++;
        }
    }

    public static void main(String[] args) {
        int[][] nums = new int[][]{{3, 2, 1}, {4, 9, 8}, {5, 6, 7}};
        hand(nums);
    }
}
